﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BLL;
using Utils;
//2011-7-29 和保险挂钩
//增加的时候，离职时间修改为保险结束时间
//更新的时候，更新保险结束时间
//删除的时候，清空保险结束时间
//保险参与离职本月工资计算（这个不用考虑）
namespace UI.ModEmployee
{
    public partial class EmpLeaveAdd : System.Web.UI.Page
    {
        EmpLeaveBiz empLeaveBiz = new EmpLeaveBiz();
        EmployeeBiz employeeBiz = new EmployeeBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        string leaveID = "0";
        string account = "";
        string isSpec = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
              //  isSpec = user.IsSpecial;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["LeaveID"] != null)
            {
                leaveID = Request.Params["LeaveID"].ToString();
                btnAdd.Enabled = false;
                txtEmpCode.ReadOnly = true;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
                btnLeave.Enabled = false;
            }
            if (!IsPostBack)
            {

                JSUtility.AddConfirm(btnDelete, "您真的要删除该员工的信息吗？");
                txtLeaveTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                if (leaveID != "0")
                {
                    BindData(leaveID);
                }
                else
                    defDicBiz.InitXMLDropDownList(ddlLeaveType, "LeaveType.xml", "自动离职");


            }
        }
        private void BindData(string leaveID)
        {
            DataTable dt = empLeaveBiz.GetOneInfo(leaveID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();

                if (dt.Rows[0]["IsSalary"].ToString() == "N")
                    cbIsSalary.Checked = true;
                else cbIsSalary.Checked = false;
        
                string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                if (Utils.RegexRule.IsDate(JoinTime))
                    txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");
                else txtJoinTime.Text = JoinTime;
                // txtLeaveTime.Text = dt.Rows[0]["LeaveTime"].ToString();
                string LeaveTime = dt.Rows[0]["LeaveTime"].ToString();
                if (Utils.RegexRule.IsDate(LeaveTime))
                    txtLeaveTime.Text = Convert.ToDateTime(LeaveTime).ToString("yyyy-MM-dd");
                else txtLeaveTime.Text = LeaveTime;

                txtDayNum.Text = dt.Rows[0]["DayNum"].ToString();
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                string leaveType = dt.Rows[0]["LeaveType"].ToString().Trim();
                if (leaveType == "请长假")
                {
                    cbIsSalary.Checked = true;
                    btnLeave.Visible = true;
                }
                else
                {
                    btnLeave.Visible = false;
                    cbIsSalary.Checked = false;
                }
                if (dt.Rows[0]["Statu"].ToString() == "N")
                {
                    txtEmpCode.AutoPostBack = false;
                    if (isSpec != "S")
                    {
                        btnUpdate.Enabled = false;
                        btnDelete.Enabled = false;
                        btnLeave.Enabled = false;
                    }

                }
                defDicBiz.InitXMLDropDownList(ddlLeaveType, "LeaveType.xml",dt.Rows[0]["LeaveType"].ToString());
                defDicBiz.InitDropDownList(ddlIsProcedure, dt.Rows[0]["IsProcedure"].ToString());
                dt.Dispose();
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();

                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();

                            string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                            if (Utils.RegexRule.IsDate(JoinTime))
                                txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");

                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();

                            string JoinTime = dt.Rows[0]["JoinTime"].ToString();
                            if (Utils.RegexRule.IsDate(JoinTime))
                                txtJoinTime.Text = Convert.ToDateTime(JoinTime).ToString("yyyy-MM-dd");

                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职，不能再次离职");
                    }
                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpLeaveAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "EmpLeaveList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }

            string leaveTime = txtLeaveTime.Text.Trim();
            if (!RegexRule.IsDate(leaveTime))
            {
                JSUtility.Alert(this, "离职时间无效");
                return;
            }

            string dayNum = txtDayNum.Text.Trim();
            if (string.IsNullOrWhiteSpace(dayNum)) dayNum = "0";
            else
            {
                if (!RegexRule.IsInt(dayNum))
                {
                    JSUtility.Alert(this, "交接天数应该输入数字");
                    return;
                }

            }
            DataTable empTb = employeeBiz.GetOneInfo(empCode);
            string stateID = "1";
            if (empTb.Rows.Count > 0) stateID = empTb.Rows[0]["StateID"].ToString();
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);

            sdList.Add("HisStateID", stateID);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());

            sdList.Add("LeaveTime", leaveTime);
            sdList.Add("LeaveType", ddlLeaveType.SelectedValue);
            sdList.Add("DayNum", dayNum);
            sdList.Add("IsProcedure", ddlIsProcedure.SelectedValue);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            sdList.Add("IsSalary", (cbIsSalary.Checked==true)?"N":"Y");
            
            string info = empLeaveBiz.Insert("Admin_Emp_Leave", sdList);
            JSUtility.Alert(this, info);

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpLeaveAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "EmpLeaveList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string leaveTime = txtLeaveTime.Text.Trim();
            if (!RegexRule.IsDate(leaveTime))
            {
                JSUtility.Alert(this, "离职时间无效");
                return;
            }
            if (string.IsNullOrWhiteSpace(leaveID))
            {
                JSUtility.Alert(this, "请先选择数据");
                return;
            }
            string dayNum = txtDayNum.Text.Trim();
            if (string.IsNullOrWhiteSpace(dayNum)) dayNum = "0";
            else
            {
                if (!RegexRule.IsInt(dayNum))
                {
                    JSUtility.Alert(this, "交接天数应该输入数字");
                    return;
                }

            }
            //DataTable empTb = employeeBiz.GetOneInfo(empCode);
            //string stateID = "1";
            //if (empTb.Rows.Count > 0) stateID = empTb.Rows[0]["StateID"].ToString();

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("LeaveID", leaveID);
            //sdList.Add("HisStateID", stateID);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("EmpCode", empCode);
            sdList.Add("LeaveTime", leaveTime);
            sdList.Add("LeaveType", ddlLeaveType.SelectedValue);
            sdList.Add("DayNum", dayNum);
            sdList.Add("IsProcedure", ddlIsProcedure.SelectedValue);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            sdList.Add("IsSalary", (cbIsSalary.Checked == true) ? "N" : "Y");
            if (leaveID != "0")
            {
                string info = empLeaveBiz.Update("Admin_Emp_Leave", sdList);
                BindData(leaveID);
                JSUtility.Alert(this, info);

            }

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (AccountBiz.GetRange(account, "EmpLeaveList.aspx") != RangeChar.Super)
            {
                JSUtility.Alert(this, "无操超级删除权限！");
                return;
            }
            if (!AccountBiz.CheckRight(account, "EmpLeaveAdd.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "EmpLeaveList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            if (leaveID != "0")
            {
                string info = empLeaveBiz.DeleteOneInfo(leaveID);
                btnDelete.Enabled = false;
                btnLeave.Enabled = false;
                btnUpdate.Enabled = false;
                JSUtility.Alert(this, info);
            }
            else
                JSUtility.Alert(this, "请先选择数据");
        }



        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

        protected void btnLeave_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpLeaveAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "EmpLeaveList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "无权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }

            string leaveTime = txtLeaveTime.Text.Trim();
            if (!RegexRule.IsDate(leaveTime))
            {
                JSUtility.Alert(this, "离职时间无效");
                return;
            }

            string dayNum = txtDayNum.Text.Trim();
            if (string.IsNullOrWhiteSpace(dayNum)) dayNum = "0";
            else
            {
                if (!RegexRule.IsInt(dayNum))
                {
                    JSUtility.Alert(this, "交接天数应该输入数字");
                    return;
                }

            }
            string leaveType = ddlLeaveType.SelectedValue;
            if (leaveType == "请长假")
            {
                JSUtility.Alert(this, "如果是请长假的人离职，请选择离职相关状态");
                return;
            }
            DataTable empTb = employeeBiz.GetOneInfo(empCode);
            string stateID = "1";
            if (empTb.Rows.Count > 0) stateID = empTb.Rows[0]["StateID"].ToString();
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);

            sdList.Add("HisStateID", stateID);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());

            sdList.Add("LeaveTime", leaveTime);
            sdList.Add("LeaveType", leaveType);
            sdList.Add("DayNum", dayNum);
            sdList.Add("IsProcedure", ddlIsProcedure.SelectedValue);
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            sdList.Add("IsSalary", (cbIsSalary.Checked == true) ? "N" : "Y");
            if (empLeaveBiz.UpdateStatuToN(empCode))
            {
                string info = empLeaveBiz.Insert("Admin_Emp_Leave", sdList);
                JSUtility.Alert(this, info);
            }
            else
                JSUtility.Alert(this, "第一步更新状态失败！");
        }
        //复职
        //protected void btnReIn_Click(object sender, EventArgs e)
        //{
        //    if (!AccountBiz.CheckRight(account, "EmpLeaveAdd.aspx", RightChar.Mod))
        //    {
        //        if (!AccountBiz.CheckRight(account, "EmpLeaveList.aspx", RightChar.Mod))
        //        {
        //            JSUtility.Alert(this, "无权限！");
        //            return;
        //        }
        //    }
        //    string empCode = txtEmpCode.Text.Trim();
        //    if (string.IsNullOrWhiteSpace(empCode))
        //    {
        //        JSUtility.Alert(this, "员工编号必须填写");
        //        return;
        //    }
        //    if (!employeeBiz.IsExistEmpCode(empCode))
        //    {
        //        JSUtility.Alert(this, "员工编号不存在");
        //        return;
        //    }
        //    string leaveTime = txtLeaveTime.Text.Trim();
        //    if (!RegexRule.IsDate(leaveTime))
        //    {
        //        JSUtility.Alert(this, "离职时间无效");
        //        return;
        //    }
        //    if (string.IsNullOrWhiteSpace(leaveID))
        //    {
        //        JSUtility.Alert(this, "请先选择数据");
        //        return;
        //    }

        //    string reInTime = txtLeaveTime2.Text;
        //    if (reInTime == "") return;
        //    string stateID = ddlStateID.SelectedValue;
        //    if (stateID == "0" || stateID == "")
        //    {
        //        JSUtility.Alert(this, "请选择入职状态");
        //        return;
        //    }

        //    if (leaveID != "0")
        //    {             
        //            //更新员工信息
        //            SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
        //            hList.Add("EmpCode", txtEmpCode.Text.Trim());
        //            hList.Add("StateID", stateID);//字典中的ID
        //            hList.Add("ReMark", System.DateTime.Now.ToString("yyyy-MM-dd") + "复职");
        //            hList.Add("LeaveTime","null");
        //            if (employeeBiz.Update("Admin_Employee", hList))
        //            {
        //                SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
        //                sdList.Add("LeaveID", leaveID);
        //                sdList.Add("Statu", "N");
        //                sdList.Add("Memo2", "复职" + reInTime);
        //                empLeaveBiz.Update(sdList);
        //                JSUtility.Alert(this, "复职成功");
        //            }
        //            else
        //                JSUtility.Alert(this, "复职失败");

        //    }
        //}
        ////返聘
        //protected void btnBack_Click(object sender, EventArgs e)
        //{
        //    if (!AccountBiz.CheckRight(account, "EmpLeaveAdd.aspx", RightChar.Mod))
        //    {
        //        if (!AccountBiz.CheckRight(account, "EmpLeaveList.aspx", RightChar.Mod))
        //        {
        //            JSUtility.Alert(this, "无权限！");
        //            return;
        //        }
        //    }
        //    try
        //    {
        //        string empCode = txtEmpCode.Text.Trim();
        //        if (string.IsNullOrWhiteSpace(empCode))
        //        {
        //            JSUtility.Alert(this, "员工编号必须填写");
        //            return;
        //        }
        //        if (!employeeBiz.IsExistEmpCode(empCode))
        //        {
        //            JSUtility.Alert(this, "员工编号不存在");
        //            return;
        //        }
        //        string leaveTime = txtLeaveTime.Text.Trim();
        //        if (!RegexRule.IsDate(leaveTime))
        //        {
        //            JSUtility.Alert(this, "离职时间无效");
        //            return;
        //        }
        //        if (string.IsNullOrWhiteSpace(leaveID))
        //        {
        //            JSUtility.Alert(this, "请先选择数据");
        //            return;
        //        }
        //       string stateID= ddlStateID.SelectedValue;
        //       if (stateID == "0" || stateID=="")
        //       {
        //           JSUtility.Alert(this, "请选择入职状态");
        //           return;
        //       }
        //        string reInTime = txtLeaveTime2.Text;
        //        if (reInTime == "") return;
        //        //应该弹出窗口修改
        //        SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
        //        hList.Add("EmpCode", empCode);
        //        hList.Add("StateID", stateID);//字典中的ID
        //        hList.Add("JoinTime", reInTime);//入职时间  以前工作抹零
        //        hList.Add("LeaveTime", "null");//离职时间XmlUtility.GetWebParam("DateOfLeaveTime")
        //        hList.Add("ReMark", reInTime + "返聘");
        //        if (employeeBiz.Update("Admin_Employee", hList))
        //        {
        //            employeeBiz.WriteEmpFlow(empCode);
        //            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
        //            sdList.Add("LeaveID", leaveID);
        //            sdList.Add("Statu", "N");
        //            sdList.Add("Memo2", "返聘" + reInTime);
        //            empLeaveBiz.Update(sdList);
        //            //保险要重新买，所以这里不需要更新
        //            JSUtility.Alert(this, "返聘成功");
        //        }
        //        else
        //            JSUtility.Alert(this, "返聘失败");
        //    }
        //    catch (Exception ee)
        //    {
        //        JSUtility.Alert(this, ee.Message);
        //    }
        //}

       //不再提供简单删除功能
        //protected void btnSimpleDel_Click(object sender, EventArgs e)
        //{
        //    if (isSpec != "S")
        //    {
        //        JSUtility.Alert(this, "无操超级删除权限！");
        //        return;
        //    }
        //    if (!AccountBiz.CheckRight(account, "EmpLeaveAdd.aspx", RightChar.Del))
        //    {
        //        if (!AccountBiz.CheckRight(account, "EmpLeaveList.aspx", RightChar.Del))
        //        {
        //            JSUtility.Alert(this, "无权限！");
        //            return;
        //        }
        //    }
        //    if (leaveID != "0")
        //    {
        //        string info = empLeaveBiz.DeleteOneInfo(leaveID, "");
        //        btnDelete.Enabled = false;
        //        btnSimpleDel.Enabled = false;
        //        btnUpdate.Enabled = false;
        //        JSUtility.Alert(this, info);
        //    }
        //    else
        //        JSUtility.Alert(this, "请先选择数据");
        //}
    }
}